<template>
	<view class="">
		<view class="search">
			<u-search placeholder="请输入商品搜索" v-model="keyword" @search="serachlist"></u-search>
		</view>
		<view class="list">
			<u-waterfall v-model="list" ref="uWaterfall">
				<template v-slot:left="{ leftList }">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @click="handlerDetails(item)">
						<!-- 微信小程序需要hx2.8.11版本才支持在template中引入其他组件，比如下方的u-lazy-load组件 -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.images_format[0]" :index="index"></u-lazy-load>
						<view class="demo-title">{{ item.name }}</view>
						<view class="demo-price">
							<view class="left">
								{{ item.price }}元
							</view>
							<view class="right">
								月销{{item.sale_count}}
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{ rightList }">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @click="handlerDetails(item)" >
						<u-lazy-load threshold="-450" border-radius="10" :image="item.images_format[0]" :index="index"></u-lazy-load>
						<view class="demo-title">{{ item.name }}</view>
						<view class="demo-price">{{ item.price }}元</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
	</view>
</template>

<script>
	import { getWaterGoods,getGoodListno2,getGoodslist} from '@/api/index.js';
	export default {
		data(){
			return{
				keyword:'',
				type:'',
				loadStatus: 'loadmore',
				flowList: [],
				page:1,
				list: [],
				type:1
			}
		},
		watch:{
			list:{
				handler(val,old){
					this.list = val
				},
				deep:true
			}
		},
		onLoad(options) {
			// this.addRandomData();
			this.type = options.type
			this.typeId = options.typeId
			if(options.title){
				this.keyword = options.title
			}
			// this.getGoodList()
			if(options.type == 'null'){
				console.log(99);
				this.getGoodList()
			}
			if(options.type != 'null'){
				console.log(88);
				this.gethotGood()
			}
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			this.page += 1
			// 模拟数据加载
			setTimeout(() => {
				this.loadStatus = 'loadmore';
				if(this.type == 'null'){
					console.log(99);
					this.getGoodList()
				}
				if(this.type != 'null'){
					console.log(88);
					this.gethotGood()
				}
			}, 1000);
		},
		methods: {
			serachlist(){
				this.page = 1
				this.list = []
				this.$refs.uWaterfall.clear();
				this.getGoodList()
			},
			getGoodList() {
				let that = this
				getGoodslist({page:this.page,keyword:this.keyword,category_id:this.typeId}).then(res=>{
						let arr = res.list;
						arr.map(item=>{
							that.list.push(item)
						})
						if(res.list.length<=10){
							that.loadStatus = 'nomore';
						}
				})
				// getGoodListno2({page:this.page,keyword:this.keyword}).then(res=>{
				// 	// console.log(888,res);
				// 	let arr = res.list;
				// 	arr.map(item=>{
				// 		that.list.push(item)
				// 	})
				// 	if(res.list.length<=10){
				// 		that.loadStatus = 'nomore';
				// 	}
				// })
			},
			gethotGood(){
				let that = this
				getGoodListno2({page:this.page,keyword:this.keyword}).then(res=>{
					// console.log(888,res);
					let arr = res.list;
					arr.map(item=>{
						that.list.push(item)
					})
					if(res.list.length<=10){
						that.loadStatus = 'nomore';
					}
				})
				// getWaterGoods({page:this.page}).then(res=>{
				// 	// console.log(888,res.data);
				// 	that.list = res.data[0].goods
				// 	if(res.data[0].goods.length<=10){
				// 		that.loadStatus = 'nomore';
				// 	}
				// })
			},
			handlerDetails(item){
				uni.navigateTo({
					url:'/pages/goods-details/goods-details?id='+ item.id
				})
			}
		}
	}
</script>


<style>
/* page不能写带scope的style标签中，否则无效 */
page {
	background-color: rgb(240, 240, 240);
}
</style>

<style lang="scss" scoped>
	.search{
		width: 100%;
		background-color: #fff;
		margin-bottom: 20upx;
		padding: 0 20upx 10upx;
		position: fixed;
		top: 0;
		z-index: 1;
	}
	.list{
		margin-top: 100upx;
	}
.demo-warter {
	border-radius: 8px;
	margin: 5px;
	background-color: #ffffff;
	padding: 8px;
	position: relative;
	.demo-price{
		display: flex;
		justify-content: space-between;
		align-items: center;
		.right{
			font-size: 24rpx;
			color: #666;
		}
	}
}

.u-close {
	position: absolute;
	top: 32rpx;
	right: 32rpx;
}

.demo-img-wrap {
}

.demo-image {
	width: 100%;
	border-radius: 4px;
}

.demo-title {
	font-size: 30rpx;
	margin-top: 5px;
	color: $u-main-color;
	word-break: break-all;
}

.demo-tag {
	display: flex;
	margin-top: 5px;
}

.demo-tag-owner {
	background-color: $u-type-error;
	color: #ffffff;
	display: flex;
	align-items: center;
	padding: 4rpx 14rpx;
	border-radius: 50rpx;
	font-size: 20rpx;
	line-height: 1;
}

.demo-tag-text {
	border: 1px solid $u-type-primary;
	color: $u-type-primary;
	margin-left: 10px;
	border-radius: 50rpx;
	line-height: 1;
	padding: 4rpx 14rpx;
	display: flex;
	align-items: center;
	border-radius: 50rpx;
	font-size: 20rpx;
}

.demo-price {
	font-size: 30rpx;
	color: $u-type-error;
	margin-top: 5px;
}

.demo-shop {
	font-size: 22rpx;
	color: $u-tips-color;
	margin-top: 5px;
}
</style>

